/*const doGetCellValue=(idName)=>
{
  var tmpObj=document.getElementById(idName);
  if(tmpObj)
    if(tmpObj.tagName=="TD")
    {
      if(tmpObj.Selector&&tmpObj.Selector!="")
      {
        if(tmpObj.value&&tmpObj.value!="")
          return tmpObj.value;
        else
          return 0;
      }
      if(tmpObj.innerText=="")
        return 0;
      else
      if(isNaN(tmpObj.innerText))
        if(tmpObj.innerText.indexOf("%")>0)
          if(isNaN(tmpObj.innerText.substring(0,tmpObj.innerText.indexOf("%"))))
            return 0;
          else
            return tmpObj.innerText.substring(0,tmpObj.innerText.indexOf("%"))/100;
        else
          return 0;
      else
        return tmpObj.innerText;
    }
    else
    if(tmpObj.value!=""&&!isNaN(tmpObj.value))
      return tmpObj.value;
    else
      return 0;
  else
    return 0;
}*/

const getQueryString=(name)=>{
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = location.search.substr(1).match(reg);
  if (r != null) return unescape(decodeURI(r[2])); return null;
}

const trimText=function (id) {
  this.document.getElementById(id).innerText=this.document.getElementById(id).innerText.trim();
}

const doCalculation=function(id){
  var strDo=new String();
  var left,middle,right,acd;
  left=middle="";
  right=$(id).attr('ACF');
  acd=$(id).attr('ACD');
  if(right.indexOf("?")>=0)
  {
    var strArray=right.substring(right.indexOf("?")+1,right.indexOf("}")).split(",");
    right="("+strArray[0]+")?("+strArray[1]+"):("+strArray[2]+")";
  }
  var status='F'
  while(right.indexOf("]")>=0){
    middle=right.substring(0,right.indexOf("]"));
    right=right.substring(right.indexOf("]")+1,right.length);
    left=middle.substring(0,middle.indexOf("["));
    middle=middle.substring(middle.indexOf("[")+1,middle.length);
    strDo+=left+"("+this.doGetCellValue(middle)+")"
    if(this.eval(middle+'.Editor')&&this.eval(middle+'.Editor').substring(0,1)=='I')
      status='I'
  }
  strDo+=right
  var tmpValue = this.eval(strDo)
  //alert("395=="+tmpValue);
  //eval("(1.83)-(1.00)-(0.83)-(0.00)")实际上并不等于0.00,而是等于?.?????e-16的数.
  //将小于0.0001的数据直接赋值为0.00/ 注:当其中的相减的数据达到 9999999999999.90时差值达到-0.00007812500000002262
  if(Math.abs(tmpValue)<0.0000001){
    tmpValue = "0.00";
  }
  if(tmpValue!=0){
    //tmpValue出现0.224999999999999999999的时候执行防止直接保留两位小数不进位
    tmpValue = new Number(tmpValue).toFixed(10);//四舍五入之前加1
  }
  if(tmpValue=="Infinity"){
    tmpValue = "0.00";
  }
  if(status=='F'||acd=='F'){

    if($(id).attr('Digit')){
      id.innerText=this.formatMoneyD(tmpValue,$(id).attr('Digit'));
    }else{
      tmpValue = new Number(tmpValue)+0.00000000001;
      tmpValue = new Number(tmpValue).toFixed(5);
      id.innerText=this.formatMoney(tmpValue);
    }
  }
  else{
    id.innerText=this.formatInt(tmpValue)
  }
}

const doPost=function(){
  this.postForm.innerHTML = ""
  var obj_hidden =this.document.getElementsByTagName("INPUT");
  if (obj_hidden.length>0)
  {
    var var_hidden="";
    for (i=0; i< obj_hidden.length; i++)
    {
      if(obj_hidden[i].type=="hidden")
      {
        var_hidden += "<input type=hidden value='" + obj_hidden[i].value + "' name=" + obj_hidden[i].id + ">"
      }
    }
    this.postForm.innerHTML += var_hidden;
  }
  var d = this.workTab.getElementsByTagName("TD")
  for(var i = 0; i < d.length; i ++)
    if(d[i].id != ""){
      this.postForm.innerHTML += "<input type=hidden value='" + d[i].innerText + "' name=" + d[i].id + ">"
    }
  this.postForm.submit()
}

function reportSaveData_ydDo(reportname, nsrsbh, nslx, keyExist) {
  let window = this;
  let document = window.document;
  if (window.ismodify != null && (window.ismodify.value == '1' || window.ismodify.value == '2' || window.ismodify.value == '4' || window.ismodify.value == '5')) {
    _app.alert('此申报表已上报，不允许再修改数据！');
    return;
  }
  console.log(this);
  var qylx = this.getRadios2('qylx');
  if (qylx != '2') {
    // 期末从业人数必填

  }
  var C1_1 = parseFloat(window.C1_1.innerHTML);
  var C2_1 = parseFloat(window.C2_1.innerHTML);
  var C3_1 = parseFloat(window.C3_1.innerHTML);
  var C4_1 = parseFloat(window.C4_1.innerHTML);
  var C5_1 = parseFloat(window.C5_1.innerHTML);
  var C6_1 = parseFloat(window.C6_1.innerHTML);
  var C7_1 = parseFloat(window.C7_1.innerHTML);
  var C8_1 = parseFloat(window.C8_1.innerHTML);
  var C9_1 = parseFloat(window.C9_1.innerHTML);
  if (C8_1 != 0 && (C3_1 + C4_1 - C5_1 - C6_1 - C7_1) <= 0) {
    _app.alert('第8行“弥补以前年度亏损”应等于0，当本表第3+4-5-6-7行≤0时');
    return;
  }
  var MBYQNDKS = parseFloat(document.getElementById('MBYQNDKS').value);
  if (C8_1 < 0 || C8_1 > MBYQNDKS) {
    _app.alert('第8行“弥补以前年度亏损”应大于等于0 且小于等于上年度结转的可弥补亏损额（' + MBYQNDKS + '）元');
    return;
  }
  if (C4_1 < 0) {
    if ((C1_1 - C2_1) < (C4_1 * (-1))) {
      _app.alert('冲回的预计利润，应小于营业收入-营业成本');
      return;
    }
  }
  // else{
  //  if(C4_1>formatTwo(C1_1-C2_1)){
  //	  _app.alert("冲回的预计利润，应小于营业收入-营业成本");
  //	  return;
  //	  }
  //  }
  var C4_1_SFTS = document.getElementById('C4_1_SFTS').value;
  if (C4_1_SFTS == 'Y' && C4_1 > 0) {
    if (!confirm('房地产开发企业销售未完工开发产品取得的预售收入，按照税收规定的预计计税毛利率计算的预计毛利额填入此行。请确认是否填写正确。')) {
      return;
    }
  }
  var C11_1 = parseFloat(window.C11_1.innerHTML);
  var C12_1 = parseFloat(window.C12_1.innerHTML);
  if (C12_1 > C11_1) {
    _app.alert('第12行“减：减免所得税额”应小于等于第11行应纳所得税额。');
    return;
  }
  if (C12_1 < 0) {
    _app.alert('第12行“减：减免所得税额”应大于等于0。');
    return;
  }
  // var xxwlqy=getRadios2("xxwlqy");
  // if(xxwlqy=="0"){
  // if(C12_1<formatTwo(C9_1*0.1-1)){
  // _app.alert("该企业符合小型微利企业优惠政策条件，在不享受其他优惠情况下，不得放弃享受请通过“减免所得税优惠明细表（A201030）”填报优惠情况。");
  // return;
  // }
  // }
  var C11_1 = parseFloat(window.C11_1.innerHTML);
  var C12_1 = parseFloat(window.C12_1.innerHTML);
  var C13_1 = parseFloat(window.C13_1.innerHTML);
  var C14_1 = parseFloat(window.C14_1.innerHTML);
  var C15_1 = parseFloat(window.C15_1.innerHTML);
  var tsnsr = '0';
  var tsnsrsl = parseFloat('false');
  function formatTwo(f){
    if(isNaN(f) || f == ""||f == 0){
      return "0.00";
      }
      f= new Number(f ).toFixed(10);    
      f = new Number(f)+0.00000000001;
      f = new Number(f).toFixed(5);//1967223.9149999998, 针对失真情况的保留两位小数的处理方式，
        var fixNum = (new Number(f)+1).toFixed(2);//四舍五入之前加1  
       var fixedNum = new Number(fixNum - 1).toFixed(2);//四舍五入之后减1，再四舍五入一下  
      return fixedNum;
   }
   function getRadios2(name){
    var temp = document.getElementsByName(name);
    for(var i=0;i<temp.length;i++){
       if(temp[i].checked)
             var param = temp[i].value;
    }
    if(undefined==param){
      param="";
    }
    return param;
  }
  function tbts(){
  var w = window.open("/report/sds2019/tbsm/tbsm_A_2019.jsp", "填表说明", "left= 90,top=60,width=700,height=500,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no");
}
  if (tsnsr == '0') {
    var jsC15_1 = formatTwo(C11_1 - C12_1 - C13_1 - C14_1);
    if (jsC15_1 < 0) {
      if (C15_1 != 0) {
        _app.alert('第15行“本期应补（退）所得税额（11-12-13-14） \ 税务机关确定的本期应纳所得税额”应等于0。');
        return;
      }
    } else if (C15_1 != jsC15_1) {
      _app.alert('第15行“本期应补（退）所得税额（11-12-13-14） \ 税务机关确定的本期应纳所得税额”应等于本表11行减12行减13行减14行。');
      return;
    }
  } else {
    var jsC15_1 = formatTwo(C11_1 * tsnsrsl - C12_1 * tsnsrsl - C13_1 - C14_1);
    if (jsC15_1 < 0) {
      if (C15_1 != 0) {
        _app.alert('第15行“本期应补（退）所得税额（11-12-13-14） \ 税务机关确定的本期应纳所得税额”应等于0。');
        return;
      }
    } else if (C15_1 != jsC15_1) {
      _app.alert('第15行“本期应补（退）所得税额（11-12-13-14） \ 税务机关确定的本期应纳所得税额”应等于本表11行乘以' + tsnsrsl + '减12行乘以' + tsnsrsl + '减13行减14行。');
      return;
    }
  }
  if (C15_1 < 0) {
    _app.alert('第15行“本期应补（退）所得税额（11-12-13-14） \ 税务机关确定的本期应纳所得税额”应大于等于0。');
    return;
  }
  var C20_1 = parseFloat(window.C20_1.innerHTML);
  if (C20_1 > 1 || C20_1 < 0) {
    _app.alert('分支机构本期分摊比例应该在0到1之间');
    return;
  }

  var SNXWBZ = document.getElementById('SNXWBZ').value;
  // var FDDBR = trim(document.getElementById("FDDBR").innerText);
  // var fddbr_s= document.getElementById("fddbr_s").value;
  var SKJZRQ_1 = document.getElementById('SKJZRQ_1').value;
  // 即所属期为3、6、9、12月）可以填报该区域信息。
  // var qylx=getRadios2("qylx");
  var gjxzhjzhy = getRadios2('gjxzhjzhy');
  if ((SKJZRQ_1.indexOf('0331') > -1 || SKJZRQ_1.indexOf('0630') > -1 || SKJZRQ_1.indexOf('0930') > -1 || SKJZRQ_1.indexOf('1231') > -1) && qylx != 2) {
    var C23_1 = parseFloat(window.C23_1.innerHTML);
    var C24_1 = parseFloat(window.C24_1.innerHTML);
    var C25_1 = parseFloat(window.C25_1.innerHTML);
    var C26_1 = parseFloat(window.C26_1.innerHTML);
    if (C23_1 <= 0) {
      _app.alert('请填写“季初从业人数”，整数，且应大于0');
      return;
    }
    if (C24_1 <= 0) {
      _app.alert('请填写“季末从业人数”，整数，且应大于0');
      return;
    }
    if (C25_1 < 0) {
      _app.alert('请填写“季初资产总额（万元）”，且应大于等于0');
      return;
    }
    if (C26_1 < 0) {
      _app.alert('请填写“季末资产总额（万元）”,且应大于等于0');
      return;
    }
    // if (C25_1 == 0) {
    //   if (!confirm('请确认季初资产总额（万元）为0')) {
    //     return;
    //   }
    // }
    // if (C26_1 == 0) {
    //   if (!confirm('请确认季末资产总额（万元）为0')) {
    //     return;
    //   }
    // }
    if (gjxzhjzhy == '') {
      _app.alert('请选择是否为“国家限制或禁止行业”');
      return;
    }
  }

  var DLJGGZ = document.getElementById('DLJGGZ').innerText;

  var JBR = document.getElementById('JBR').innerText;
  var alertTs = '<div style="text-align:left">';
  var C17_2 = document.getElementById('C17_2').value;
  var C18_2 = document.getElementById('C18_2').value;
  var C19_2 = document.getElementById('C19_2').value;
  var C19_3 = document.getElementById('C19_3').value;
  var yjfs = getRadios2('yjfs');

  var xxwlqy = getRadios2('xxwlqy');
  var kjxzxqy = getRadios2('kjxzxqy');
  if (kjxzxqy == '' && qylx != '2') {
    _app.alert('请选择是否为“科技型中小企业”');
    return;
  }
  var gxjsqy = getRadios2('gxjsqy');
  if (gxjsqy == '' && qylx != '2') {
    _app.alert('请选择是否为“高新技术企业”');
    return;
  }
  var jsrgdynssx = getRadios2('jsrgdynssx');
  if (jsrgdynssx == '' && qylx != '2') {
    _app.alert('请选择是否为“技术入股递延纳税事项企业”');
    return;
  }

  var jsrgdynssx = getRadios2('jsrgdynssx');
  if (jsrgdynssx == '' && qylx != '2') {
    _app.alert('请选择是否为“技术入股递延纳税事项企业”');
    return;
  }
  window.doPost = () =>{
    this.postForm.innerHTML = ""
    var obj_hidden =document.getElementsByTagName("INPUT");
    if (obj_hidden.length>0)
    {
      var var_hidden="";
      for (i=0; i< obj_hidden.length; i++)
      {
        if(obj_hidden[i].type=="hidden")
        {
          var_hidden += "<input type=hidden value='" + obj_hidden[i].value + "' name=" + obj_hidden[i].id + ">"
        }
      }
      this.postForm.innerHTML += var_hidden;
    }
    var d = this.workTab.getElementsByTagName("TD")
    for(var i = 0; i < d.length; i ++)
      if(d[i].id != ""){
        this.postForm.innerHTML += "<input type=hidden value='" + d[i].innerText + "' name=" + d[i].id + ">"
      }
      this.postForm.submit()
  }
  if (alertTs.length < 40) {
    window.postForm.method = 'POST';
    window.postForm.action = '/ReportJdAction_2019.do?activity=saveData&reportname=' + reportname + '&nsrsbh=' + nsrsbh + '&taxpayerType=' + nslx + '&yjfs=' + yjfs + '&qylx=' + qylx + '&xxwlqy=' + xxwlqy + '&kjxzxqy=' + kjxzxqy + '&gxjsqy=' + gxjsqy + '&jsrgdynssx=' + jsrgdynssx + '&C17_2=' + C17_2 + '&C18_2=' + C18_2 + '&C19_2=' + C19_2 + '&C19_3=' + C19_3 + '&GJXZHJZHY=' + gjxzhjzhy;
    document.postForm.target = '_self';
    window.doPost();
  } else {
    _app.alert(alertTs, function() {
      window.postForm.method = 'POST';
      window.postForm.action = '/ReportJdAction_2019.do?activity=saveData&reportname=' + reportname + '&nsrsbh=' + nsrsbh + '&taxpayerType=' + nslx + '&yjfs=' + yjfs + '&qylx=' + qylx + '&xxwlqy=' + xxwlqy + '&kjxzxqy=' + kjxzxqy + '&gxjsqy=' + gxjsqy + '&jsrgdynssx=' + jsrgdynssx + '&C17_2=' + C17_2 + '&C18_2=' + C18_2 + '&C19_2=' + C19_2 + '&C19_3=' + C19_3 + '&GJXZHJZHY=' + gjxzhjzhy;
      document.postForm.target = '_self';
      window.doPost();
    });
  }
}


const sendSbList=(currentZeroInfo,arr)=>{
  let sbAllList=[
    {
      declareName:"增值税(适用于一般纳税人)",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"地方教育附加",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"教育费附加",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"城市维护建设税",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"财务报表(年度)",
      declareCycle:"year",
      accountBook:"",
      declareTime:"",
      isSuccess:false,
    },
    {
      declareName:"企业所得税(年报)",
      declareCycle:"year",
      accountBook:"",
      declareTime:"",
      isSuccess:false,
    },
  ];
  sbAllList.forEach((item,index)=>{
    const date=new Date(),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate();
    if(item.declareCycle==='month'){
      item.accountBook=moment().subtract(1, 'months').format("YYYY-MM");
      item.declareTime=moment().format("YYYY-MM-DD");
    }else if(item.declareCycle==='year'){
      item.accountBook=moment().subtract(1, 'years').format("YYYY");
      item.declareTime=moment().format("YYYY-MM-DD");
    }
    if(index===0){
      item.isSuccess=arr[0];
    }
    if(index>0 && index <4){
      item.isSuccess=arr[1];
    }
    if(index===4){
      item.isSuccess=arr[2];
    }
    if(index===5){
      item.isSuccess=arr[3];
    }
  });

  let backInfo = {
    ...currentZeroInfo,
    recordList: sbAllList,
  };
  // 零申报结束
  window.postMessage(
    {
      event: 'zeroReportBack',
      data: backInfo,
    },
    '*'
  );
};

export {
  getQueryString,
  trimText,
  doCalculation,
  doPost,
  sendSbList,
  reportSaveData_ydDo
};
